package com.stress.testing.plugin.java.jdbc.interceptor;

import com.stress.testing.common.conf.AgentConfig;
import com.stress.testing.common.conf.PluginConfig;
import com.stress.testing.common.interceptor.AbstractInterceptor;
import com.stress.testing.plugin.java.jdbc.wrapper.ConnectionWrapper;

import java.sql.Connection;
import java.util.Properties;

public class DriverInterceptor extends AbstractInterceptor {
    @Override
    public Object after(Object result, Object[] args, Object zuper, Object thiz) {
        if (args == null ||
                args.length != 2 ||
                !(args[0] instanceof String) ||
                !(args[1] instanceof Properties)) {
            return result;
        }

        AgentConfig agentConfig = getAgentConfig();
        PluginConfig pluginConfig = getPluginConfig();
        String url = (String) args[0];
        if (result != null &&
                result instanceof Connection &&
                !(result instanceof ConnectionWrapper)) {
            result = new ConnectionWrapper((Connection) result, url, agentConfig, pluginConfig);
        }

        return result;
    }
}
